import {getRolePageList,getUserResetPassword,getDeptUserRoles,getUserDetail,getUserEnable} from "@/api/api.js"
export const initSys = {
    data() {
        return {
            options: {
                disabledDate(date) {
                    return date < Date.now();
                }
            },
            pswModal:false,
            psw:"",
            pswagain:"",
            pswid:"",
            roleList: [],
            textFlag:false,
            passwordagain:"",
            username: "",
            password: "",
            email: "",
            phone: "",
            nickname: "",
            expireTime:'',
            type:"2",
            userModal:false,
            typeFlag:false,
        }
    },
    methods:{
        getTime(value){
            this.expireTime = value;
        },
        //角色
        getRoleList(keyWord,deptId,flag,type){
            let params = {
                keyword :keyWord || "",
                deptId:deptId ,
            };
            getRolePageList(params)
                .then((res) => {
                    if (res.code == 200) {
                        this.roleList = res.data.list
                        if(this.total >  this.totalCount) {
                            this.total = this.totalCount;
                        }
                        if(this.roleList.length>0){
                            if(flag) {
                                if(type == 1) {
                                    this.roleId =this.roleId|| this.roleList[0].id;
                                } else if (type == 2 || type == 3) {
                                    this.roleId =  this.roleList[0].id;
                                }
                                this.roleList.forEach(i=>{
                                    if(this.roleId == i.id){
                                        this.Titlename = i.name;
                                    }
                                })
                                let count = Number(this.tabActiveName);
                                if (count == 1) {
                                    let s= type ==3 ?true:false
                                    this.updateHTRow(s);
                                } else if (count == 2) {
                                    this.updateXCXRow();
                                } else if(count == 3) {
                                    this.RolerUserlist(this.roleId);
                                }
                            }
                        }
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },

        //账号重置密码
        resetpsw(index, rows) {
            this.pswModal = true;
            this.pswid = rows[index].id;
            this.psw = "";
            this.pswagain = "";
        },
        reset(type) {
            if (this.psw == "" || this.pswagain == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "密码不能为空"
                });
                return;
            }
            if (this.psw != this.pswagain) {
                this.$Modal.warning({
                    title: "错误",
                    content: "两次输入的密码不一致"
                });
                return;
            }
            let params = {
                userId: this.pswid,
                password: this.psw
            };
            getUserResetPassword(params)
                .then(res => {
                    if (res.code == 200) {
                        this.$Message.success("操作成功");
                        this.pageNum = 1;
                        this.pswModal = false;
                        if(type){
                            this.getuserList(null);
                        }

                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        //账号启用禁用
        updateStatus(type, index, rows,bol) {
            let params = {
                userId: rows[index].id,
                status: type?1:0
            };
            getUserEnable(params)
                .then(res => {
                    if (res.code == 200) {
                        this.$Message.success("操作成功");
                        this.pageNum = 1;
                        if(bol) {
                            this.getuserList(null);
                        }
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        //获取部门角色列表
        getUserRoles(userId,deptId){
            let params = {
                userId:userId,
                deptId:deptId,
            }
            getDeptUserRoles(params).then(res=>{
                if(res.code == 200) {
                    if(deptId != '') {
                        res.data.list[0].roleList.forEach(i=>{
                            this.roleIds.push(i.id)
                        })
                    } else {
                       this.roleUserList = res.data.list;
                    }
                } else {

                }
            }).catch(error=>{
                console.log(error)
            })
        },
        getEmpUserRoles(empId,deptId){
            let params = {
                empId:empId,
                deptId:deptId,
            }
            getDeptUserRoles(params).then(res=>{
                if(res.code == 200) {
                    if(deptId != '') {
                        res.data.list[0].roleList.forEach(i=>{
                            this.roleIds.push(i.id)
                        })
                    } else {
                       this.roleUserList = res.data.list;
                    }
                } else {

                }
            }).catch(error=>{
                console.log(error)
            })
        },
        //获取账号详情
        getUserDetails(id){
            let params = {
                userId:id,
            }
            getUserDetail(params).then(res=>{
                if(res.code == 200) {
                    this.username = res.data.username;
                    this.nickname = res.data.nickname;
                    this.password = res.data.password;
                    this.phone = res.data.phone;
                    this.email = res.data.email;
                    this.type = String(res.data.type);
                    this.expireTime = res.data.expireTime;
                    this.userType = res.data.userType;
                    if(new Date().getTime() > Date.parse(new Date(this.expireTime)) ){
                        this.textFlag = true;
                    } else {
                        this.textFlag = false;
                    }
                } else {
                    this.$Message.warning(res.msg);
                }
            }).catch(error=>{
                console.log(error)
            })
        },
        //获取账号详情
        getEmpUserDetails(id){
            let params = {
                empId:id,
            }
            getUserDetail(params).then(res=>{
                if(res.code == 200) {
                    // console.log('getEmpUserDetails:'+JSON.stringify(res.data))c
                    this.userId = res.data.id;
                    this.username = res.data.username;
                    this.nickname = res.data.nickname;
                    this.password = res.data.password;
                    this.phone = res.data.phone;
                    this.email = res.data.email;
                    this.type = String(res.data.type);
                    this.expireTime = res.data.expireTime;
                    if(new Date().getTime() > Date.parse(new Date(this.expireTime)) ){
                        this.textFlag = true;
                    } else {
                        this.textFlag = false;
                    }
                } else {
                    this.$Message.warning(res.msg);
                }
            }).catch(error=>{
                console.log(error)
            })
        }
    },

}
